import { COMMIT_PRODUCT, DELETE_PRODUCT, GET_PRODUCT, GET_PRODUCTS } from "@/graphql/product";
import { DEFAULT_PAGE_SIZE } from "@/utils/contants";
import type {
  TBaseProduct,
  TProductQuery,
  TProductsQuery,
} from "@/utils/types";
import { useLazyQuery, useMutation, useQuery } from "@apollo/client";
import { message } from "antd";
import { useMemo } from "react";

export const useProducts = (
  pageNum = 1,
  pageSize = DEFAULT_PAGE_SIZE
  // isSample = false
) => {
  const { loading, data, refetch } = useQuery<TProductsQuery>(GET_PRODUCTS, {
    skip: true, //因为主要是由refetch触发，这里需要这样配置，避免两次请求
    variables: {
      page: {
        pageNum,
        pageSize,
      },
    },
  });

  const refetchHandler = async (params: {
    name?: string;
    pageSize?: number;
    current?: number;
  }) => {
    const { data: res, error } = await refetch({
      name: params.name,
      page: {
        pageNum: params.current,
        pageSize: params.pageSize,
      },
    });
    console.log("refetchHandler-data", res);
    if (error) {
      return {
        success: false,
      };
    }
    return {
      success: true,
      total: res?.getProducts.page.total,
      data: res?.getProducts.data,
    };
  };

  return {
    loading,
    refetch: refetchHandler,
    page: data?.getProducts.page,
    data: data?.getProducts.data,
  };
};

export const useEditProductInfo = (): [
  handleEdit: (
    id: string,
    params: TBaseProduct,
    callback?: () => void
  ) => Promise<void>,
  loading: boolean
] => {
  const [edit, { loading }] = useMutation(COMMIT_PRODUCT);

  const handleEdit = async (
    id: string,
    params: TBaseProduct,
    callback?: (isReload: boolean) => void
  ) => {
    const res = await edit({
      variables: {
        id,
        params,
      },
    });
    if (res.data.commitProduct.code === 200) {
      message.success(res.data.commitProduct.message);
      if (callback) callback(true);
      return;
    }

    message.error(res.data.commitProduct.message);
  };

  return [handleEdit, loading];
};

export const useProduct = () => {
  // const { loading, data } = useQuery<TProductQuery>(GET_PRODUCT, {
  //   skip: true,
  //   variables: {
  //     id,
  //   },
  // });

  // 手动触发请求
  const [get, { loading }] = useLazyQuery(GET_PRODUCT);
  const getProduct = async (id: string) => {
    const res = await get({
      variables: {
        id,
      },
    });
    return res.data.getProductInfo.data;
  };
  return {
    loading,
    getProduct,
  };
};
export const useProductInfo = (id: string) => {
  const { loading, data, refetch } = useQuery<TProductQuery>(GET_PRODUCT, {
    skip: !id,
    variables: {
      id,
    },
  });
  const newData = useMemo(() => {
    const res = data?.getProductInfo.data;
    return {
      ...res,
      coverUrl: [{
        url: res?.coverUrl,
      }],
      bannerUrl: [{
        url: res?.bannerUrl,
      }],
    };
  }, [data]);

  return {
    loading,
    refetch,
    data: data?.getProductInfo.data ? newData:undefined,
  };
};

export const useDeleteProduct = (): [
  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
  delHandler: (id: string, callback?: Function) => Promise<void>,
  loading: boolean
] => {
  const [del, { loading }] = useMutation(DELETE_PRODUCT);

  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
  const delHandler = async (id: string, callback?: Function) => {
    const res = await del({
      variables: {
        id,
      },
    });
    if (res.data?.deleteProduct.code === 200) {
      message.info(res.data.deleteProduct.message);
      if(callback)
        callback();
      return;
    }
    message.info(res.data.deleteProduct.message);
  };

  return [delHandler, loading];
};
